package com.zlt.controller;

import com.zlt.dto.AdminDTO;
import com.zlt.entity.Admin;
import com.zlt.entity.Company;
import com.zlt.entity.License;
import com.zlt.entity.Question;
import com.zlt.service.AdminService;
import com.zlt.util.*;
import com.zlt.vo.Pager;
import com.zlt.vo.Result;
import redis.clients.jedis.Jedis;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

@WebServlet("/admin/*")
public class AdminController extends BaseController {

    private final AdminService service = AdminService.getInstance();

    @Override
    protected void defaultMethod(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }


    /**
     * 返回管理员登录时的验证码
     *
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result codeImage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        VerifyCode verifyCode = new VerifyCode();
//        验证码图片
        BufferedImage image = verifyCode.verifyImage();
//        验证码
        String code = verifyCode.getVerifyCode();
//        生成UUID
        String uuid = StringUtil.uuid();
//        将验证码保存到Redis中,设置存活时间为300 seconds
        Jedis jedis = JedisUtil.getConnection();
        jedis.setex(uuid, 300, code);
//        将Redis中key响应给客户端
        resp.setHeader("Token", uuid);
//        返回Base64加密的图片字符串
        return Result.ok(StringUtil.changeImageToString(image));
    }

    /**
     * 管理员登录
     *
     * @param request
     * @param response
     * @return
     */
    protected Result login(HttpServletRequest request, HttpServletResponse response) {
        AdminDTO dto = ClassUtil.resolveRequest(AdminDTO.class, request);
        String token = request.getHeader("token");
        Jedis jedis = JedisUtil.getConnection();
        String s = jedis.get(token);
        if (StringUtil.isNotNull(s) && s.equalsIgnoreCase(Objects.requireNonNull(dto).getCaptcha())) {
            Admin username = service.isExistedUsername(dto);
            if (username == null) {
                return Result.error("用户名或者密码错误");
            } else {
                String md5 = MD5Util.getMD5(dto.getPassword(), username.getSalt(), 4);
                if (md5.equals(username.getPassword())) {
                    //登录成功，让客户端保存登录token
                    String sign = JWTUtil.sign(username.getUsername(), username.getPassword());
                    return Result.ok("登录成功", sign);
                } else
                    return Result.error("用户名或者密码错误");
            }
        } else {
            return Result.error("验证码错误");
        }
    }

    /**
     * 查询公司信息分页显示
     *
     * @param request
     * @param response
     * @return
     */
    protected Result companyInfo(HttpServletRequest request, HttpServletResponse response) {
        String page = request.getParameter("page");//当前页
        String limit = request.getParameter("limit");//当前页条数
        Pager<Company> pager = service.companyInfo(Integer.parseInt(page), Integer.parseInt(limit));
        if (pager == null) {
            return Result.ok("没有记录");
        }
        return Result.ok("成功", pager);
    }

    /**
     * 查看指定状态的公司信息
     *
     * @param request
     * @param response
     * @return
     */
    protected Result companyStateInfo(HttpServletRequest request, HttpServletResponse response) {
        String page = request.getParameter("page");//当前页
        String limit = request.getParameter("limit");//当前页条数
        String state = request.getParameter("state");//状态码
        Pager<Company> pager = service.companyInfo(Integer.parseInt(page), Integer.parseInt(limit), Integer.parseInt(state));
        if (pager == null) {
            return Result.ok("没有记录");
        }
        return Result.ok("成功", pager);
    }

    /**
     * 查询所有的反馈
     *
     * @param request
     * @param response
     * @return
     */
    protected Result listQuestions(HttpServletRequest request, HttpServletResponse response) {
        String page = request.getParameter("page");//当前页
        String limit = request.getParameter("limit");//当前页条数
        Pager<Question> pager = service.listQuestions(Integer.parseInt(page), Integer.parseInt(limit), true, 0, 1);
        if (pager == null) {
            return Result.ok("没有记录");
        }
        return Result.ok("成功", pager);
    }

    /**
     * 查询指定状态的反馈
     *
     * @param request
     * @param response
     * @return
     */
    protected Result questionStateInfo(HttpServletRequest request, HttpServletResponse response) {
        String page = request.getParameter("page");//当前页
        String limit = request.getParameter("limit");//当前页条数
        String state = request.getParameter("state");//状态码
        Pager<Question> pager = service.listQuestions(Integer.parseInt(page), Integer.parseInt(limit), false, Integer.parseInt(state));
        if (pager == null) {
            return Result.ok("没有记录");
        }
        return Result.ok("成功", pager);
    }

    /**
     * 改变企业审核状态
     *
     * @param request
     * @param response
     * @return
     */
    protected Result changeCompanyState(HttpServletRequest request, HttpServletResponse response) {
        String cid = request.getParameter("cid");
        String state = request.getParameter("state");
        Integer integer = service.changeCompanyState(Integer.parseInt(cid), Integer.parseInt(state));
        if (integer > 0) {
            return Result.ok("审核通过");
        } else return Result.error("修改失败");
    }

    /**
     * 查询公司许可证
     *
     * @param request
     * @param response
     * @return
     */
    protected Result companyLicenses(HttpServletRequest request, HttpServletResponse response) {
        String cid = request.getParameter("cid");
        List<License> licenses = service.findLicenses(Integer.parseInt(cid));
        if (licenses == null) {
            return Result.error("查询错误");
        } else return Result.ok("查询成功", licenses);
    }

    protected Result search(HttpServletRequest request, HttpServletResponse response) {
        String page = request.getParameter("page");//当前页
        String limit = request.getParameter("limit");//当前页条数
        String address = request.getParameter("address");
        String state = request.getParameter("state");
        String clegal = request.getParameter("clegal");

        return null;
    }
}
